package edu.uwm.universitydb.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class Login extends JPanel {
	
	public Login() {
		this.setLayout(new BorderLayout());
		
		JPanel titlePanel = new JPanel();
		titlePanel.setLayout(new FlowLayout(FlowLayout.LEFT, 16, 5));
		titlePanel.setBackground(Color.WHITE);
		titlePanel.setBorder(BorderFactory.createMatteBorder(0, 0, 16, 0, Color.WHITE));
		JLabel title = new JLabel("Login");
		title.setFont(new Font(title.getFont().getName(), title.getFont().getStyle(), 18));
		titlePanel.add(title);
		this.add(titlePanel, BorderLayout.NORTH);
		
		this.add(this.createAppView(), BorderLayout.CENTER);
		
	}
	
	private JPanel createAppView() {
		JPanel outer = new JPanel(new FlowLayout());
		outer.setBackground(Color.WHITE);
		JPanel fields = new JPanel(new GridLayout(0, 1));
		fields.setBackground(Color.WHITE);

		// DB URL
		fields.add(new JLabel("Database URL:"));
		_dburl = new JTextField();
		_dburl.setName(DBURLFIELD);
		_dburl.setPreferredSize(new Dimension(270,24));
		_dburl.setText("jdbc:mysql://localhost:3306/university");
		fields.add(_dburl);
		
		//Username
		fields.add(new JLabel("Username:"));
		_username = new JTextField();
		_username.setName(USERNAMEFIELD);
		fields.add(_username);
		
		//Password
		fields.add(new JLabel("Password"));
		_password = new JPasswordField();
		_password.setName(PASSWORDFIELD);
		fields.add(_password);
		fields.add(Box.createRigidArea(new Dimension(0,16)));
		
		_signInButton = new JButton("Sign in"); 
		_signInButton.setName(SIGNINBUTTON);
		fields.add(_signInButton);
		
		outer.add(fields);
		
		return outer;
	}
	
	public void resetFields() {
		_username.setText("");
		_password.setText("");
	}
	
	public JButton getSigninButton() {
		return _signInButton;
	}
	
	public JPasswordField getPasswordField() {
		return _password;
	}
	
	public String getDbUrl() {
		return _dburl.getText();
	}
	
	public String getUsername() {
		return _username.getText();
	}
	
	public String getPassword() {
		return new String(_password.getPassword());
	}
	
	private JButton _signInButton;
	private JTextField _dburl;
	private JTextField _username;
	private JPasswordField _password;
	public static final String DBURLFIELD = "DBURL";
	public static final String USERNAMEFIELD = "USERNAME";
	public static final String PASSWORDFIELD = "PASSWORD";
	public static final String SIGNINBUTTON = "SIGNIN";
	private static final long serialVersionUID = 8109943420708300543L;
	
}
